﻿package fr.oz.init
{
	import caurina.transitions.properties.ColorShortcuts;
	import caurina.transitions.properties.FilterShortcuts;
	import caurina.transitions.Tweener;
	import flash.desktop.NativeApplication;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.utils.setTimeout;
	import fps.FPS;
	import fr.digitas.flowearth.conf.Conf;
	import fr.oz.core.datas.ProfilList;
	import fr.oz.core.datas.UserProfiles;
	import fr.oz.core.events.ModuleEvent;
	import fr.oz.mobile.common.ModuleManager;
	import fr.oz.mobile.home.Home;
	import fr.oz.mobile.ui.PanelMenu;
	import fr.oz.view.bg.BackgroundManager;
	
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class Main extends Sprite 
	{
		private var panelMenu:PanelMenu;
		private var moduleManager:ModuleManager;
		private var home:Home;
		
		private var debug:Boolean = false;
		private var fps:FPS;
		
		public function Main()
		{
			if (stage)	init( null );
			else 		addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			FilterShortcuts.init();
			ColorShortcuts.init();
			
			var userProfiles:ProfilList = new ProfilList();
			userProfiles.addEventListener(Event.COMPLETE, displayUI);
			userProfiles.init(Conf.getDatas("wakeUpProfile").data.userDataProfiles);
			
		}
		
		private function displayUI(e:Event):void
		{
			panelMenu = new PanelMenu();
			panelMenu.y = stage.stageHeight;
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyPressed);
			
			this.graphics.beginFill(0xff0000);
			this.graphics.drawCircle(0, stage.stageHeight, 4);
			this.graphics.drawCircle(stage.stageWidth, stage.stageHeight, 4);
			this.graphics.endFill();
			
			
			var bg:BackgroundManager = new BackgroundManager();
			addChild(bg);
			
			setTimeout(createHome, 200);
			
		}
		
		private function createHome():void{
			home = new Home();
			home.addEventListener(ModuleEvent.MODULE_SELECTED, setView);
			addChild(home);
		}
		
		private function handleKeyPressed(e:KeyboardEvent):void 
		{
			e.preventDefault();
			switch(e.keyCode) {
				case Keyboard.MENU:
					trace("panelMenu.y = " + panelMenu.y);
					if (panelMenu.open) {
						closeMenu();
					}else {
						openMenu();
					}
				break;
				case Keyboard.BACK:
					if (moduleManager != null) {
						if (!moduleManager.goBack()) {
							rebuildHome();
						}
					}else {
						NativeApplication.nativeApplication.exit(0);
					}
				break;
				case Keyboard.SEARCH:
					if (!debug) {
						fps = new FPS();
						addChild(fps);
					}else {
						removeChild(fps);
						fps = null;
					}
				break;
			}
		}
		
		private function rebuildHome():void
		{
			Tweener.addTween(moduleManager, { alpha:0, time:0} );
			home = new Home();
			home.addEventListener(ModuleEvent.MODULE_SELECTED, setView);
			home.alpha = 0;
			addChild(home);
			Tweener.addTween(home, {alpha:1, time:1, onComplete:shutDownModuleManager } );
		}
		
		private function shutDownModuleManager():void
		{
			removeChild(moduleManager);
			moduleManager = null;
		}
		
		private function setView(e:ModuleEvent):void
		{
			if (moduleManager == null) {
				
				moduleManager = new ModuleManager();
			}
			//moduleManager.alpha = 0;
			if (!moduleManager.setModule(e.id))	return;
			addChild(moduleManager);
			
			if (home != null)
			{
				home.removeView();
				home.addEventListener(Event.CLOSE, handleKillModule);
			}
		}
		
		private function handleKillModule(e:Event):void 
		{
			//var module:Sprite = e.currentTarget as Sprite;
			//removeChild(module);
			//module = null;
			home.removeEventListener(Event.CLOSE, handleKillModule);
			removeChild(home);
			home = null;
			trace("HOME KILLED");
		}
		
		private function openMenu():void
		{
			trace("open");
			addChild(panelMenu);
			panelMenu.open = true;
			Tweener.addTween(panelMenu, {y:stage.stageHeight - panelMenu.height, time:1 } );
		}
		
		private function closeMenu():void
		{
			trace("close");
			panelMenu.open = false;
			Tweener.addTween(panelMenu, {y:stage.stageHeight, time:1 } );
		}
		
	}
	
}